/**
 * 
 */
package fr.cephb.servlet;


import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.events.EndElement;

import fr.cephb.util.XMLUtilities;


/**
 * Extends AbstractTabularExportFilter
 * for exporting the result as XML
 *
 */
public class XMLExportFilter extends AbstractTabularExportFilter
	{
	/** return true if req.getParameter(PARAM)=="xml" */
	@Override
	protected boolean isFiltering(HttpServletRequest req)
			throws ServletException
		{
		
		boolean b= "xml".equalsIgnoreCase(req.getParameter(PARAM));
		return b;
		}
	
	@Override
	protected void export(File content, HttpServletRequest req,
			HttpServletResponse res) throws IOException, ServletException {
		res.setContentType("text/xml");
		super.export(content, req, res);
		}
	
	@Override
	public void endCell(HttpServletRequest req, HttpServletResponse res,EndElement e, int row,
			int col, String content) throws IOException, ServletException {
		if(col>0) res.getWriter().print('\t');
		res.getWriter().print("<col>"+XMLUtilities.escape(content.trim())+"</col>");
		}
	
	@Override
	public void startRow(HttpServletRequest req, HttpServletResponse res,
			int line) throws IOException, ServletException {
		res.getWriter().print("<row>");
		}
	
	@Override
	public void endRow(HttpServletRequest req, HttpServletResponse res, int line)
			throws IOException, ServletException {
		res.getWriter().print("</row>");
		}

	@Override
	public void startTable(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException
		{
		res.getWriter().print("<?xml version=\'1.0\' encoding='UTF-8'?>\n");
		res.getWriter().print("<table>");
		}
	
	@Override
	public void endTable(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException {
		res.getWriter().print("</table>");
		res.getWriter().flush();
		}
	
	}
